package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.ProxyListMapper;
import com.so206.mapper.ProxyRecordMapper;
import com.so206.mapper.SystemUserMapper;
import com.so206.mapper.ext.ProxySelectExtMapper;
import com.so206.mapper.ext.SystemCountMapper;
import com.so206.po.ProxyList;
import com.so206.po.ProxyRecord;
import com.so206.po.SystemUser;
import com.so206.po.ext.ProxyCountModel;
import com.so206.po.ext.ProxySelectExt;
import com.so206.service.ProxyService;
import com.so206.utils.BeanCheck;
import com.so206.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 代理相关
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ProxyServiceImpl implements ProxyService {

    @Autowired
    private ProxyListMapper proxyListMapper;

    @Autowired
    private ProxySelectExtMapper proxySelectExtMapper;

    @Autowired
    private ProxyRecordMapper proxyRecordMapper;

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private SystemCountMapper systemCountMapper;


    @Override
    public PageInfo<ProxySelectExt> find_by_page(Integer page, Integer rows, Long uid, Integer status, String user) {
        PageHelper.startPage(page, rows);
        if (user != null && user.length() == 0) {
            user = null;
        }
        if (status != null && status == -1) {
            status = null;
        }
        List<ProxySelectExt> list = proxySelectExtMapper.find_proxy_list(uid, status, user);
        return new PageInfo<>(list);
    }

    @Override
    public Long save_proxy(ProxyList proxyList) {
        proxyList.setCreateTime(new Date());
        proxyList.setOptTime(new Date());
        proxyListMapper.insert(proxyList);
        return proxyList.getId();
    }

    @Override
    public ProxyList find_proxy_by_id(Long id) {
        return proxyListMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update_proxy(ProxyList proxyList) {
        ProxyList model = proxyListMapper.selectByPrimaryKey(proxyList.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(proxyList, model);
            model.setOptTime(new Date());
            proxyListMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public void delete_proxy(Long id) {
        proxyListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ProxyList find_proxy_by_uid(Long uid) {
        List<ProxyList> list = proxyListMapper.selectByUid(uid);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<ProxyRecord> find_record_by_page(Integer page, Integer rows, Long uid, Long sub_uid, Long order_id) {
        PageHelper.startPage(page, rows);
        List<ProxyRecord> list = proxyRecordMapper.selectByUidOrderIdSubId(uid, order_id, sub_uid);
        return new PageInfo<>(list);
    }

    @Override
    public ProxyRecord find_record_by_id(Long id) {
        return proxyRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public void delete_record(Long id) {
        proxyRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long save_record(ProxyRecord proxyRecord) {
        proxyRecord.setCreateTime(new Date());
        proxyRecord.setOptTime(new Date());
        proxyRecordMapper.insert(proxyRecord);
        return proxyRecord.getId();
    }

    @Override
    public void update_record(ProxyRecord proxyRecord) {
        ProxyRecord model = proxyRecordMapper.selectByPrimaryKey(proxyRecord.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(proxyRecord, model);
            model.setOptTime(new Date());
            proxyRecordMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public List<ProxyCountModel> count_proxy(Integer status, Long uid, String timelike, String endTime) {
        List<ProxyCountModel> ret = new ArrayList<>();
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ProxyList> list = proxyListMapper.selectByUidStatus(uid, status);
        if (list != null && list.size() > 0) {
            for (ProxyList proxyList : list) {
                ProxyCountModel model = new ProxyCountModel();
                BeanCheck.copyPropertiesIgnoreNull(proxyList, model);
                //用户名
                Long model_uid = proxyList.getUid();
                SystemUser user = systemUserMapper.selectByPrimaryKey(model_uid);
                if (user != null) {
                    model.setUser(user.getUser());
                }
                //分润钱
                BigDecimal proxy_count = systemCountMapper.query_proxy_count_money(model_uid, utils.getStartTime(), utils.getEndTime());
                model.setChange_sett_count(proxy_count == null ? new BigDecimal("0.00") : proxy_count);
                //下级用户数
                int proxy_count_user = systemCountMapper.query_proxy_count_user(model_uid, utils.getStartTime(), utils.getEndTime());
                model.setSub_user(proxy_count_user);
                //已支付订单数,已支付金额,手续费
                {
                    int order_create = 0;
                    BigDecimal order_money = new BigDecimal("0.00");
                    BigDecimal order_channge = new BigDecimal("0.00");
                    List<SystemUser> users = systemUserMapper.selectByUserParent(model_uid);
                    for (SystemUser systemUser : users) {
                        Integer order = systemCountMapper.todayCreateOrder(systemUser.getId(), 2, utils.getStartTime(), utils.getEndTime(), null);
                        if (order != null) {
                            order_create += order;
                        }
                        BigDecimal decimal = systemCountMapper.todayOrderSettMoneyCount(systemUser.getId(), 2, utils.getStartTime(), utils.getEndTime());
                        if (decimal != null) {
                            order_channge = order_channge.add(decimal);
                        }
                        BigDecimal moneyCount = systemCountMapper.todayOrderMoneyCount(systemUser.getId(), 2, utils.getStartTime(), utils.getEndTime(), null);
                        if (moneyCount != null) {
                            order_money = order_money.add(moneyCount);
                        }
                    }
                    Integer order = systemCountMapper.todayCreateOrder(model_uid, 2, utils.getStartTime(), utils.getEndTime(), null);
                    if (order != null) {
                        order_create += order;
                    }
                    BigDecimal decimal = systemCountMapper.todayOrderSettMoneyCount(model_uid, 2, utils.getStartTime(), utils.getEndTime());
                    if (decimal != null) {
                        order_channge = order_channge.add(decimal);
                    }
                    BigDecimal moneyCount = systemCountMapper.todayOrderMoneyCount(model_uid, 2, utils.getStartTime(), utils.getEndTime(), null);
                    if (moneyCount != null) {
                        order_money = order_money.add(moneyCount);
                    }
                    model.setOrder_count(order_create);
                    model.setOrder_money(order_money);
                    model.setOrder_change(order_channge);
                }
                ret.add(model);
            }
        }
        return ret;
    }



}
